/*
@功能：购物车页面js
@作者：diamondwang
@时间：2013年11月14日
*/



$(function(){


    function total() {
        //总计金额
        var total = 0;
        $(".col5 span").each(function(){
            total += parseFloat($(this).text());
        });

        $("#total").text(total.toFixed(2));
    }




	//减少
	$(".reduce_num").click(function(){


        var tr = $(this).closest('tr');

        var goods_id = tr.attr('data-goods-id');
        var csrftoken = tr.find(".csrftoken").val();

        var amount = tr.find(".amount");

		if (parseInt($(amount).val()) <= 1){
			alert("商品数量最少为1");
		} else{
				var num = parseInt($(amount).val() -1);

            	$(amount).val(num);

				var data={'_csrf-frontend':csrftoken,goods_id:goods_id,num:num};
            $.post('/shop/update-cart',data,function () {
                //小计
                var subtotal = parseFloat(tr.find(".col3 span").text()) * parseInt($(amount).val());
                tr.find(".col5 span").text(subtotal.toFixed(2));

                total();

			});
		}

	});

	//增加
	$(".add_num").click(function(){

		var tr = $(this).closest('tr');

		var goods_id = tr.attr('data-goods-id');
		var csrftoken = tr.find(".csrftoken").val();

        var amount = tr.find(".amount");
        var num = parseInt($(amount).val())+1;
        $(amount).val(num);

		var data={'_csrf-frontend':csrftoken,goods_id:goods_id,num:num};

		$.post('/shop/update-cart',data,function () {
            //小计
            var subtotal = parseFloat(tr.find(".col3 span").text()) * parseInt($(amount).val());
            tr.find(".col5 span").text(subtotal.toFixed(2));
            //总计金额
	          total();
        });



	});

	//直接输入
	$(".amount").blur(function(){
        var tr = $(this).closest('tr');
        var amount =$(this);

		if (parseInt($(this).val()) < 1){
			alert("商品数量最少为1");
			$(this).val(1);
		}

        var goods_id = tr.attr('data-goods-id');
        var csrftoken = tr.find(".csrftoken").val();
        var num = $(this).val();
        var data={'_csrf-frontend':csrftoken,goods_id:goods_id,num:num};

        $.post('/shop/update-cart',data,function (data) {
            if(data == 'success') {
                //小计
                var subtotal = parseFloat(tr.find(".col3 span").text()) * parseInt($(amount).val());
                tr.find(".col5 span").text(subtotal.toFixed(2));
                //总计金额
                total();
            }
        });
	});

	$(".delOneGoods").click(function () {
	    if(confirm('确定删除吗?')) {
            var tr = $(this).closest('tr');
            var goods_id = tr.attr('data-goods-id');
            var csrftoken = tr.find(".csrftoken").val();
            var data = {'_csrf-frontend': csrftoken, goods_id: goods_id};
            $.post('/shop/del-cart', data, function (data) {
                if (data == 'success') {
                    tr.remove();
                    total();
                }
            });
        }

    })


});